package com.linzm.leetcode.primary.exercises1_20230114;

/**
 * @Author zimingl
 * @Date 2023/1/14 14:01
 * @Description: 移除指定数字得到的最大结果
 *
 * 做法：遍历判断目标数据后面的值是否大于它
 */
public class Demo4_2259 {
    /**
     * 输入：number = "1231", digit = "1"
     * 输出："231"
     * 解释：可以移除第一个 '1' 得到 "231" 或者移除第二个 '1' 得到 "123" 。
     * 由于 231 > 123 ，返回 "231" 。
     */

    public static void main(String[] args) {
        String number = "1332353";
//        String number = "299858953917872714814599237991174513476623756395992135212546127959342974628712329595771672911914471";
//        String number = "1231";
        char digit = '3';
        Demo4_2259 demo4_2259 = new Demo4_2259();
        String max = demo4_2259.removeDigit(number, digit);
        System.out.println(max);
    }

    public String removeDigit(String number, char digit) {
        StringBuilder sb = new StringBuilder(number);
        int i = 0;
        int max = -1;
        for (; i < number.length(); i++) {
            if (number.charAt(i) == digit) {
                max = i;
                if(i == number.length() - 1 || number.charAt(i + 1) >  digit){
                    break;
                }
            }
        }
        sb.deleteCharAt(max);
        return sb.toString();
    }

    public String removeDigit2(String number, char digit) {
        StringBuilder stringBuilder = new StringBuilder(number);
        int max = -1;
        for (int i = 0; i < number.length(); i++) {
            if (number.charAt(i) == digit) {
                max = i;
                if (i == number.length() - 1 || digit < number.charAt(i + 1)) {
                    break;
                }
            }
        }
        return stringBuilder.deleteCharAt(max).toString();
    }
}
